{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ce1ecf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2e90d77",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_path = '../data/nuscenes/class_count.pkl'\n",
    "with open(info_path, 'rb') as f:\n",
    "    data_info = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3750da7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0002, 0003, 0008, 0017, 0018, 0039, 0045, 0060, 0063, 0073"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124f1af2",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_info[70]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8faa4d0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_info[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4d06c64",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_path = '../data/nuscenes/nuscenes_data_info.pkl'\n",
    "with open(info_path, 'rb') as f:\n",
    "    data_info = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc4b7f99",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(data_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf26ac30",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pickle\n",
    "import numpy as np\n",
    "from nuscenes.nuscenes import NuScenes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "344905e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(np.array([1, 2, 3]) ** 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e96ae657",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_root = '../data/nuscenes'\n",
    "info_path = '../data/nuscenes/nuscenes_data_info.pkl'\n",
    "nusc = NuScenes(version='v1.0-trainval-select', dataroot=data_root, verbose=False)\n",
    "with open(info_path, 'rb') as f:\n",
    "    data_info = pickle.load(f)\n",
    "classes = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 5: 'bus', 7: 'truck'}\n",
    "cams = data_info[0]['samples'][0]['cams'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d07f7329",
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_idx = 0\n",
    "idx = 15\n",
    "scene_save_dir = os.path.join(data_root, '3d_objects', data_info[scene_idx]['scene_name'])\n",
    "sample = data_info[scene_idx]['samples'][idx]\n",
    "results_path = os.path.join(scene_save_dir, os.path.basename(sample['lidar_path']).replace('.bin', '.pkl'))\n",
    "with open(results_path, 'rb') as f:\n",
    "    results = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88352938",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_points_dict = {\n",
    "    'person': [],\n",
    "    'bicycle': [],\n",
    "    'car': [],\n",
    "    'motorcycle': [],\n",
    "    'bus': [],\n",
    "    'truck': []\n",
    "}\n",
    "\n",
    "for scene_idx in range(len(data_info)):\n",
    "    scene_save_dir = os.path.join(data_root, '3d_objects', data_info[scene_idx]['scene_name'])\n",
    "    for idx in range(len(data_info[scene_idx]['samples'])):\n",
    "        sample = data_info[scene_idx]['samples'][idx]\n",
    "        results_path = os.path.join(scene_save_dir, os.path.basename(sample['lidar_path']).replace('.bin', '.pkl'))\n",
    "        with open(results_path, 'rb') as f:\n",
    "            results = pickle.load(f)\n",
    "        for obj in results['objects']:\n",
    "            name = obj['name'].split('_')[0]\n",
    "            if obj.get('points') is not None:\n",
    "                num_points = obj['points'].shape[0]\n",
    "            else:\n",
    "                num_points = 0\n",
    "            num_points_dict[name].append(num_points)\n",
    "\n",
    "for name in num_points_dict:\n",
    "    num_points = np.array(num_points_dict[name])\n",
    "    print(f'{name}: {num_points.mean():.2f} ({num_points.std():.2f})')\n",
    "    print(f'  {name} == 0: {np.sum(num_points == 0)}')\n",
    "    print(f'  0 < {name} <= 5: {np.sum((num_points > 0) & (num_points <= 5))}')\n",
    "    print(f'  5 < {name} <= 10: {np.sum((num_points > 5) & (num_points <= 10))}')\n",
    "    print(f'  10 < {name} <= 20: {np.sum((num_points > 10) & (num_points <= 20))}')\n",
    "    print(f'  20 < {name} <= 50: {np.sum((num_points > 20) & (num_points <= 50))}')\n",
    "    print(f'  {name} > 50: {np.sum(num_points > 50)}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "089673a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_count = {}\n",
    "for scene_idx in range(len(data_info)):\n",
    "    for idx in range(len(data_info[scene_idx]['samples'])):\n",
    "        sample = data_info[scene_idx]['samples'][idx]\n",
    "        for obj in sample['labels']:\n",
    "            name = obj['name']\n",
    "            if name not in labels_count:\n",
    "                labels_count[name] = 0\n",
    "            labels_count[name] += 1\n",
    "            \n",
    "for name in labels_count:\n",
    "    print(f'{name}: {labels_count[name]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a09736fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "for name in num_points_dict:\n",
    "    num_points = np.array(num_points_dict[name])\n",
    "    print(f'{name}: {np.sum(num_points > 10)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6c77e8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "results_count = {}\n",
    "for scene_idx in range(len(data_info)):\n",
    "    scene_save_dir = os.path.join(data_root, '3d_objects', data_info[scene_idx]['scene_name'])\n",
    "    for idx in range(len(data_info[scene_idx]['samples'])):\n",
    "        sample = data_info[scene_idx]['samples'][idx]\n",
    "        results_path = os.path.join(scene_save_dir, os.path.basename(sample['lidar_path']).replace('.bin', '.pkl'))\n",
    "        with open(results_path, 'rb') as f:\n",
    "            results = pickle.load(f)\n",
    "        for obj in results['objects']:\n",
    "            name = obj['name']\n",
    "            if name not in results_count:\n",
    "                results_count[name] = 0\n",
    "            results_count[name] += 1\n",
    "\n",
    "for name in results_count:\n",
    "    print(f'{name}: {results_count[name]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b85e94df",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
